package com.xbongbong.pro.statistic.service.result;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.statistic.enums.IndicatorRuleEnum;
import com.xbongbong.pro.statistic.enums.SystemCodeEnum;
import com.xbongbong.pro.statistic.result.pojo.ChartResultPojo;
import com.xbongbong.pro.statistic.result.pojo.dto.StatisticResultPieInfoDTO;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartResultDetailVO;
import com.xbongbong.pro.statistic.result.pojo.vo.ResultDataSeriesChartVO;
import com.xbongbong.pro.statistic.through.pojo.dto.ThroughDTO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.dictionary.DictionaryEnum;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 退货退款相关
 * @author zcp
 * @version v1.0
 * @date 2019/3/21 22:17
 * @since v1.0
 */
@Component
public class CrmRefundResult extends ChartResultParentService implements ChartResultSystem {
    @Resource
    private CrmProductResult crmProductResult;

    @Override
    public Object getTarget(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            case RED_REFUND_REASON_CHART:
                return redRefundReasonChart(chartResultPojo, chartResultDetailVO);
            case RED_REFUND_PRODUCT_CHART_TOP:
                return redRefundProductChartTop(chartResultPojo, chartResultDetailVO);
            default:
                break;
        }
        return null;
    }

    @Override
    public Object getValueTarget(ChartResultPojo chartResultPojo) {
        return null;
    }

    /**
     * 退货原因
     * @param chartResultPojo
     * @param chartResultDetailVO
     * @return java.lang.Object
     * @throws XbbException
     * @author zcp
     * @date 2019/3/30 17:59
     * @since v1.0
     * @version v1.0
     */
    public Object redRefundReasonChart(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        String corpid = chartResultPojo.getCorpid();
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.refundQueryBuilder(false, chartResultPojo);
        String refundReason = RefundEnum.getEsAttr4Keyword(RefundEnum.REFUND_REASON);
        //查询出退货退款的表单id
        PaasFormEntityExt paasFormEntityExt = paasFormModel.getByBusinessType(XbbRefTypeEnum.REFUND.getCode(), corpid);
        //退货formId
        Long formId = paasFormEntityExt == null ? null : paasFormEntityExt.getId();
        List<ItemPoJo> refundReasonList = dataDictionaryModel.getDictionaryList4Saas(DictionaryEnum.REFUND_REASON, corpid, formId);

        List<ResultDataSeriesChartVO> list = systemChartHelp.getDataDictionaryDataSeries(IndexTypeEnum.IDX_SAAS_REFUND, boolQueryBuilder, refundReason, refundReasonList);
        String tableName = I18nMessageUtil.getMessage(IndicatorRuleEnum.RED_REFUND_REASON.getName());

        ThroughDTO throughDto = new ThroughDTO(chartResultPojo, chartResultPojo.getSystemCodeEnum());
        StatisticResultPieInfoDTO statisticResultPieInfoDto = new StatisticResultPieInfoDTO(tableName, list, throughDto);
        List<StatisticResultPieInfoDTO> pieInfoDtoList = Collections.singletonList(statisticResultPieInfoDto);
        return systemChartHelp.packageResult4Pie(tableName, pieInfoDtoList, chartResultPojo, chartResultDetailVO);
    }

    /**
     * 退货产品TOP10
     *
     * @param chartResultPojo 入参
     * @param chartResultDetailVO 出参
     * @return java.lang.Object
     * @throws XbbException findByScroll时抛出的异常
     * @author 徐俊杰
     * @date 2019/4/4 13:42
     * @since v1.0
     */
    private Object redRefundProductChartTop(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        //限制红冲合同
        Integer isRed = 1;
        String seriesName = I18nMessageUtil.getMessage(IndicatorRuleEnum.RED_REFUND_PRODUCT.getName());
        return crmProductResult.contractProductTop(chartResultPojo, chartResultDetailVO, isRed, seriesName,true);
    }
}
